<?php

class admin_model extends CI_Model  {

    protected $_instance = array(
        'table' => 'site_admin',
        'primaryKey' => 'id',
    );

    public $_data = null;

    public function login($username, $password) {
        $username = trim($username);
		$password = md5(trim($password));
		$query = $this->db->query('SELECT * FROM ' . $this->_instance['table'] . ' WHERE username LIKE "'.$username.'" AND password LIKE "'.$password.'" ORDER BY '. $this->_instance['primaryKey'] . ' DESC LIMIT 0, 1');
		$data = $query->row();
		$query->free_result();
		if (!empty($data)) {
            $this->set_online($data->id);
			return $data;
		}
		return false;
    }

    public function set_online($id, $on = 1) {
        $time = time();
        $this->db->query('UPDATE ' . $this->_instance['table'] . ' SET is_online = '.$on.', last_login = '.$time.' WHERE id = ' . $id);
        return $this;
    }

    public function set_offline($id) {
        $this->set_online($id, 0);
        return $this;
    }

    public function get_collection($start, $limit, $conds = null, $orders = null) {
        $query = 'SELECT r.id AS role_id, r.name AS role_name, a.id, a.name AS name, a.username, a.is_active, a.email FROM site_admin AS a INNER JOIN site_role AS r ON a.role_id = r.id';
        if (is_array($conds)) {
            $conds = implode(' AND ', $conds);
        }
        if (!empty($conds)) {
            $query .= ' WHERE ' . $conds;
        }
        if (!empty($orders)) {
            $query .= ' ORDER BY ' . $orders;
        } else {
            $query .= ' ORDER BY a.id DESC';
        }
        if (!empty($limit)) {
            $query .= ' LIMIT ' . $start . ', ' . $limit;
        }
        $query = $this->db->query($query);
        $list_admin = array();
        foreach ($query->result() as $row) {
            $list_admin[] = $row;
        }
        $query->free_result();
        return $list_admin;
    }
	
	public function get_login_url() {
		return site_url('admin/login', true);
	}

    public function get_users() {
        $query = $this->db->query('SELECT r.id AS role_id, r.name AS role_name, a.* FROM site_admin AS a INNER JOIN site_role AS r ON a.role_id = r.id ORDER BY a.id DESC');
        $list_admin = array();
        foreach ($query->result() as $row) {
            $list_admin[] = $row;
        }
        $query->free_result();
        return $list_admin;
    }

    public function get_list() {
        $query = $this->db->query('SELECT id, username, name FROM site_admin ORDER BY id DESC');
        $list_admin = array();
        foreach ($query->result() as $row) {
            $list_admin[] = $row;
        }
        $query->free_result();
        return $list_admin;
    }

    public function add_admin($data) {
        $CI = & get_instance();
        if (empty($data['name'])) {
            throw new Exception('Tên người dùng không được để trống');
            return false;
        }
        $data['creator_id'] = $CI->session->userdata('admin_id');
        $data['created_at'] = time();
        $data['updated_at'] = time();
        if (empty($data['is_active'])) {
            $data['is_active'] = 0;
        }
        if (empty($data['is_system'])) {
            $data['is_system'] = 0;
        }
        if (empty($data['error'])) {
            $this->db->insert($this->_instance['table'], $data);
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }

    public function update_admin($id, $data) {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        if (empty($data['name'])) {
            throw new Exception('Tên người dùng không được để trống');
            return false;
        }
        $data['creator_id'] = $CI->session->userdata('admin_id');
        $data['updated_at'] = time();
        if (empty($data['is_active'])) {
            $data['is_active'] = 0;
        }
        if (empty($data['is_system'])) {
            $data['is_system'] = 0;
        }
        if (empty($data['error'])) {
            $this->db->where('id', $id);
            $this->db->update($this->_instance['table'], $data);
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }

    public function load_by_id($id) {
        $query = $this->db->get_where($this->_instance['table'], array('id' => $id), 1, 0);
        $this->_data = $query->row();
        if (isset($this->_data->category_ids) && !empty($this->_data->category_ids)) {
            $this->_data->category_ids = explode(',', $this->_data->category_ids);
        }
        if (!isset($this->_data->category_ids) || empty($this->_data->category_ids)) {
            $this->_data->category_ids = array();
        }
        if (is_string($this->_data->category_ids)) {
            $this->_data->category_ids = array($this->_data->category_ids);
        }
        $query->free_result();
        return $this->_data;
    }

    public function exist_username($username) {
        return $this->exist_data('username', $username);
    }

    public function exist_email($email) {
        return $this->exist_data('email', $email);
    }

    public function delete_admin($id) {
        $this->db->delete($this->_instance['table'], array('id' => $id));
        return $this;
    }

    public function get_id() {
        $CI = & get_instance();
        return $CI->session->userdata('admin_id');
    }

    public function get_admin() {
        $CI = & get_instance();
        return $CI->session->userdata('admin');
    }

    public function is_admin() {
        $admin = $this->get_admin();
        if (empty($admin) || empty($admin->role_id)) {
            return false;
        }
        if ($admin->role_id == 1) {
            return true;
        }
        return false;
    }

    public function auto_render($data) {
        if (!empty($data['category_ids'])) {
            $data['category_ids'] = implode(',', $data['category_ids']);
        } else {
            $data['category_ids'] = '';
        }
        return $data;
    }

    public function get_privileges() {
        if (!empty($_SESSION['admin_privileges_stored'])) {
            $privileges = @unserialize($_SESSION['admin_privileges_stored']);
            if (!is_array($privileges)) {
                return null;
            }
            return $privileges;
        }
        return null;
    }

    public function has_privilege($url) {
        $admin = $this->get_admin();
        if ($admin->role_id == 1) {
            return true;
        }
        if (empty($admin)) {
            return false;
        }
        $privileges = $this->get_privileges();
        if (isset($privileges[$url])) {
            return true;
        }
        return false;
    }

    public function get_password($id) {
        $query = $this->db->query('SELECT password FROM site_admin WHERE id = '.$id.' ORDER BY id DESC LIMIT 0, 1');
        $row = $query->row();
        $query->free_result();
        return $row->password;
    }

    public function get_categories() {
        $admin = $this->get_admin();
        return get_field($admin, 'category_ids');
    }
}

?>